﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiServer.Core;

namespace EPiSugar.EntityAdapter
{
    public static class PageDataExtensions 
    {
        public static IEnumerable<T> ToEntities<T>(this IEnumerable<PageData> pages) where T:class 
        {
            EntityModel model = Current.Model;
            foreach (PageData page in pages)
            {
                T entity = model.GetEntity(page) as T;
                if(entity != null)
                    yield return entity;
            }
        }

        public static T ToEntity<T>(this PageData page) where T : class
        {
            T entity = Current.Model.GetEntity(page) as T;

            if (entity == null) throw new ApplicationException("Could retrieve page " + page.PageName + "#" + page.PageLink + " as an entity of type " + typeof(T).FullName);

            return entity;
        }
    }
}
